package com.jr9z.jrjz.controller;

import com.github.pagehelper.PageHelper;
import com.jr9z.jrjz.common.BaseResponse;
import com.jr9z.jrjz.common.ResponseBuilder;
import com.jr9z.jrjz.entity.ClassicCase;
import com.jr9z.jrjz.service.ClassicCaseService;
import com.jr9z.jrjz.utils.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author: MrZcc
 * @Description:经典案例管理controller
 * @Date: Created in 16:32 2019-3-28
 * @Modified By:
 */

@Controller
@RequestMapping("/classicCase")
public class ClassicCaseController {


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ClassicCaseService classicCaseService;


    /**
     * 跳转到经典案例管理页面
     *
     * @return
     */
    @RequestMapping("/classicCaseManage")
    public String classicCaseManage() {

        logger.info("进入经典案例管理");
        return "service/classicCaseManage";
    }


    /**
     * 跳转到经典案例新增页面
     *
     * @return
     */
    @RequestMapping("/classicCaseAdd")
    public String classicCaseAdd() {

        logger.info("进入经典案例新增");
        return "service/classicCaseAdd";
    }

    /**
     * 跳转到经典案例预览页面
     *
     * @return
     */
    @RequestMapping("/classicCasePreview")
    public String classicCasePreview() {

        logger.info("进入经典案例预览");
        return "service/casePreview";
    }


    /**
     * @Description:删除经典案例
     * @date: 16:39 2019-3-28
     * @param: classicId 案例id
     * @return: 响应结果以json格式返回
     */
    @RequestMapping(value = "/admin/deleteClassic", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse deleteClassicCase(Integer classicId) {

        int result = 0;
        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            result = classicCaseService.deleteByPrimaryKey(classicId);

            custom = Constant.RESULT(result);
        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }


    /**
     * @Description:添加经典案例
     * @date: 16:45 2019-3-28
     * @param: classicCase json格式的经典案例实体
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/admin/addClassic", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse addClassicCase(@RequestBody ClassicCase classicCase) {
        int result = 0;

        ResponseBuilder custom = ResponseBuilder.custom();
        try {
            //插入数据，返回插入的条数
            result = classicCaseService.insertSelective(classicCase);
            //结果判断
            custom = Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
            System.out.println(e.toString());
        }
        return custom.build();
    }

    /**
     * 更新经典案例排序
     *
     * @param sortStr   操作字符（down---下移，up---上移）
     * @param classicId 案例id
     * @return
     */
    @RequestMapping(value = "/admin/updateSort", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse updateSort(String sortStr, Integer classicId) {

        ResponseBuilder custom = ResponseBuilder.custom();
        try {
            if (sortStr.equals("down")) {
                classicCaseService.moveDown(classicId);
            } else if (sortStr.equals("up")) {
                classicCaseService.moveUp(classicId);
            }

            custom.success(Constant.SUCCESS, Constant.SUCCESS_CODE);
        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }

    /**
     * 获取经典案例列表（分页）
     *
     * @param currPage 当前页
     * @param pageSize 每页数量
     * @return
     */
    @RequestMapping(value = "/admin/getClassicList", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse adminGetClassicCaseList(@RequestParam Integer currPage,
                                                @RequestParam Integer pageSize) {

        ResponseBuilder custom = ResponseBuilder.custom();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        try {
            List<ClassicCase> classicCaseList = classicCaseService.getClassicList();

            Long count = classicCaseService.getClassicCount();

            if (count != 0 && classicCaseList != null) {
                custom.data(classicCaseList).currPage(currPage)
                        .pageSize(pageSize).totalCount(count.intValue());
            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * 获取经典案例列表（分页）
     *
     * @param currPage 当前页
     * @param pageSize 每页数量
     * @return
     */
    @RequestMapping(value = "/getClassicList", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getClassicCaseList(@RequestParam Integer currPage,
                                           @RequestParam Integer pageSize) {

        ResponseBuilder custom = ResponseBuilder.custom();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        try {
            List<ClassicCase> classicCaseList = classicCaseService.getClassicList();

            Long count = classicCaseService.getClassicCount();

            if (count != 0 && classicCaseList != null) {
                custom.data(classicCaseList).currPage(currPage)
                        .pageSize(pageSize).totalCount(count.intValue());
            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * 根据id获取经典案例详情
     *
     * @param classicId 案例id
     * @return
     */
    @RequestMapping(value = "/getClassicById", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getClassicCaseById(Integer classicId) {
        ClassicCase classicCase;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            classicCase = classicCaseService.selectByPrimaryKey(classicId);

            if (classicCase == null) {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            } else {
                custom.data(classicCase).success(Constant.SUCCESS, Constant.SUCCESS_CODE);
            }
        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * 根据id获取经典案例详情
     *
     * @param classicId 案例id
     * @return
     */
    @RequestMapping(value = "/admin/getClassicById", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse adminGetClassicCaseById(Integer classicId) {
        ClassicCase classicCase;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            classicCase = classicCaseService.selectByPrimaryKey(classicId);

            if (classicCase == null) {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            } else {
                custom.data(classicCase).success(Constant.SUCCESS, Constant.SUCCESS_CODE);
            }
        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }


    /**
     * 更新经典案例信息
     *
     * @param classicCase json格式的景点案例实体
     * @return
     */
    @RequestMapping(value = "/admin/updateClassicById", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse updateClassicById(@RequestBody ClassicCase classicCase) {

        int result = 0;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {

            result = classicCaseService.updateByPrimaryKeySelective(classicCase);

            Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * 获取上一条经典案例
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/getPrev", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getPrev(Integer classicId) {

        ClassicCase classicCase;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {

            classicCase = classicCaseService.getPrev(classicId);

//            Constant.RESULT(result);
            custom.data(classicCase).success(Constant.SUCCESS, Constant.SUCCESS_CODE);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * 获取下一条经典案例
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/getNext", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getNext(Integer classicId) {

        ClassicCase classicCase;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {

            classicCase = classicCaseService.getNext(classicId);

//            Constant.RESULT(result);
            custom.data(classicCase).success(Constant.SUCCESS, Constant.SUCCESS_CODE);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

}
